﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.LopPlat.UI.Code
{
    public class DataGridViewComboBoxExEditingControl : ComboBox, IDataGridViewEditingControl
    {
        protected int rowIndex;
        protected DataGridView dataGridView;
        protected bool valueChanged = false;

        protected override void OnTextChanged(System.EventArgs e)
        {
            base.OnTextChanged(e);
            NotifyDataGridViewOfValueChange();
        }

        private void NotifyDataGridViewOfValueChange()
        {
            valueChanged = true;
            dataGridView.NotifyCurrentCellDirty(true);
        }
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            NotifyDataGridViewOfValueChange();
        }

        public Cursor EditingPanelCursor
        {
            get { return Cursors.IBeam; }
        }

        public DataGridView EditingControlDataGridView
        {
            get { return dataGridView; }
            set { dataGridView = value; }
        }

        public object EditingControlFormattedValue
        {
            set
            {
                Text = value.ToString();
                NotifyDataGridViewOfValueChange();
            }
            get
            {
                return this.Text;
            }
        }

        public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return Text;
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.Escape:
                case Keys.Enter:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            if (selectAll)
            {
                SelectAll();
            }
            else
            {
                this.SelectionStart = this.ToString().Length;
            }
        }
        public virtual bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        public int EditingControlRowIndex
        {
            get { return this.rowIndex; }
            set { this.rowIndex = value; }
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }

        public bool EditingControlValueChanged
        {
            get { return valueChanged; }
            set { this.valueChanged = value; }
        }
    }

    //定制该扩展列的单元格
    public class DataGridViewComboBoxExCell : DataGridViewTextBoxCell
    {
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            DataGridViewComboBoxExEditingControl clt = DataGridView.EditingControl as DataGridViewComboBoxExEditingControl;

            DataGridViewComboBoxExColumn col = (DataGridViewComboBoxExColumn)OwningColumn;

            clt.DataSource = col.DataSource;
            clt.DisplayMember = col.DisplayMember;
            clt.ValueMember = col.ValueMember;

            clt.Text = Convert.ToString(this.Value);
        }

        public override Type EditType
        {
            get
            {
                return typeof(DataGridViewComboBoxExEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                return "";
            }
        }
    }

    //定制该扩展列
    public class DataGridViewComboBoxExColumn : DataGridViewColumn
    {
        private object dataSoruce = null;

        public object DataSource
        {
            get { return dataSoruce; }
            set { dataSoruce = value; }
        }
        private string valueMember;

        public string ValueMember
        {
            get { return valueMember; }
            set { valueMember = value; }
        }
        private string displayMember;

        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value; }
        }

        public DataGridViewComboBoxExColumn()
            : base(new DataGridViewComboBoxExCell())
        {

        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewComboBoxExCell)))
                {
                    throw new InvalidCastException("is not DataGridViewComboxExCell");
                }
                base.CellTemplate = value;
            }
        }
        private DataGridViewComboBoxExCell ComboBoxCellTemplate
        {
            get
            {
                return (DataGridViewComboBoxExCell)this.CellTemplate;
            }
        }
    }
}
